import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs/internal/Observable';
import { map } from 'rxjs/operators';

import { ApiService, JsonResult } from './api.service';
import { PageVo, ContainerListVo, ContainerDetailVo, Count, ContainerListsVo, AddVo } from 'src/app/shared/models';


@Injectable({
  providedIn: 'root'
})
export class ContainerService {
  constructor(private apiService: ApiService) { }

  getContainerPage(pageIndex: number = 1, pageSize: number = 10, filter: string): Observable<PageVo<ContainerListVo>> {
    if (filter !== null && filter !== undefined && filter !== '') {
      if (filter.length > 1 && filter.length < 50) {
        const params = new HttpParams()
          .append('pageIndex', `${pageIndex}`)
          .append('pageSize', `${pageSize}`)
          .append('filter', filter);
        return this.apiService.get('/containers', { params })
          .pipe(
            map((jsonResult: JsonResult<PageVo<ContainerListVo>>) => jsonResult.data)
          );

      }
    } else {
      const params = new HttpParams()
        .append('pageIndex', `${pageIndex}`)
        .append('pageSize', `${pageSize}`);
      return this.apiService.get('/containers', { params })
        .pipe(
          map((jsonResult: JsonResult<PageVo<ContainerListVo>>) => jsonResult.data)
        );
    }
  }
  /**
   * 过滤方法
   * @param filter 过滤参数
   * @param pageIndex 页码
   * @param pageSize 条数
   * @param order 按某值排序
   * @param sort 降序/升序
   */
  /* getContainerFilterPage(filter: string, pageIndex: number = 1, pageSize: number = 10,
      order: string = 'name', sort: string = 'desc'): Observable<PageVo<ContainerListVo>> {
      const params = new HttpParams()
          .append('pageIndex', `${pageIndex}`)
          .append('pageSize', `${pageSize}`)
          .append('filter', filter)
          .append('order', order)
          .append('sort', sort);
      return this.apiService.get('/containers', { params })
          .pipe(
              map((jsonResult: JsonResult<PageVo<ContainerListVo>>) => jsonResult.data)
          );
  } */

  /**
   * 容器列表详情
   */
  getContainerDetailPage(containerId: string): Observable<PageVo<ContainerDetailVo>> {
    return this.apiService.get('/containers/' + containerId, {})
      .pipe(
        map((jsonResult: JsonResult<PageVo<ContainerDetailVo>>) => jsonResult.data)
      );
  }

  getCount(): Observable<Count> {
    return this.apiService.get('/data/containerandbaseline/count')
      .pipe(
        map((jsonResult: JsonResult<Count>) => jsonResult.data)
      );
  }

  getContainerList(pageIndex: number = 1, pageSize: number = 10): Observable<PageVo<ContainerListsVo>> {
    const params = new HttpParams()
      .append('pageIndex', `${pageIndex}`)
      .append('pageSize', `${pageSize}`);
    return this.apiService.get('/data/containers', { params })
      .pipe(
        map((jsonResult: JsonResult<PageVo<ContainerListsVo>>) => jsonResult.data)
      );
  }

  addContainer(resData: any) {
    console.log('== resData ==', resData);
    return this.apiService.post('/data/container/add', resData).pipe(
      map((jsonResult: JsonResult<AddVo>) => jsonResult)
    );
  }


}
